{% extends "layout.html" %}

{% block title %} 注册 {% endblock %}


{% block body %}

    <link type="text/css" rel="stylesheet" href="{{ url_for("static",filename='css/sign_in.css') }}">


    <div class="container" style="margin-top: 250px; ">

        <div class="row">
            <div class="col-md-offset-4 col-md-4 ">
                <p><input class="sign_in_input" id="username" placeholder="请输入用户名" autofocus></p>
                <p><input class="sign_in_input" id="password" type="password" placeholder="请输入密码"></p>
                <p><input class="sign_in_input" id="password2" type="password" placeholder="请确认密码"></p>
                <div class="input-icon form-group-lg">
                    <label style="display: block" class="select">
                        <select id="platform_name_select" class="form-control">
                            <option value="none" selected disabled>请关注一个平台</option>
                        </select>
                    </label>
                </div>
                <button class="btn btn-lg btn-warning btn-block" style="margin-top:10px;opacity: 0.9"
                        onclick="sign_in()">注 册
                </button>
            </div>

        </div>
    </div>


    <script>

        $(document).ready(function () {
            $.getJSON("/ptpx/platform_name_list", function (data) {
                platform_name_list = data.platform_name_list
                for (var i = 0; i < platform_name_list.length; ++i) {
                    platform_name = platform_name_list[i];
                    $("#platform_name_select").append(" <option value='" + platform_name + "'>" + platform_name + "</option>")
                }
            });
        });

        function sign_in() {

            var username = $('#username')[0].value;
            var password = $('#password')[0].value;
            var password2 = $('#password2')[0].value;
            var platform_name = $('#platform_name_select')[0].value

            if (username.length == 0) {
                my_alert("请输入用户名！")
                return;
            }
            if (password.length == 0) {
                my_alert("请输入密码！")
                return;
            }
            if (password2.length == 0) {
                my_alert("请确认密码！")
                return;
            }
            if (platform_name == 'none') {
                my_alert("请关注一个平台！")
                return
            }
            if (password != password2) {
                my_alert("两次输入的密码不一致，请重新输入！")
                return;
            }

            var url = "/register/username=" + username + "&password=" + password + "&platform_name="+platform_name;
            $.getJSON(url, function (data) {
                if (data.result == 0) {
                    my_alert("用户名已经存在！")
                } else {
                    my_alert("注册成功！即将跳转到登录页面...")
                    //$('#username')[0].value = '';
                    //$('#password')[0].value = '';
                    //$('#password2')[0].value = '';
                    window.setTimeout(function(){ location.href = "/sign_in"; },2500);
                }
            });
        }
    </script>
{% endblock %}
